#include "accounthandler.h"
#include "db/dbmanager.h"
#include "utils/encodetool.h"
#include "log.h"

static Logger::ptr lvqiang = LoggerMgr::GetInstance()->GetLogger("lvqiang");

void AccountHandler::SetAccountModel(const void* data, int size)
{
    accountModel.ParseFromArray(data, size);
}

void AccountHandler::Process(Player::ptr& player)
{
    Socket::ptr socket = player->GetSocket();
    switch (accountModel.accountcode())      
    {
    case proto::Register_CREQ :
        {
            proto::AccountRequest accountRequest;
            accountRequest.ParseFromArray(accountModel.msg().c_str(), accountModel.msg().size());
            std::string userName = accountRequest.username();
            std::string passWord = accountRequest.password();
            bool isSuccess = DBManager::GetInstance()->CreateUser(userName, passWord);
            proto::AccountRespose registerResponse;
            if(isSuccess == true)
            {
                registerResponse.set_issuccess(1001);
            }
            else
            {
                registerResponse.set_issuccess(1002);
            }
            accountModel.set_accountcode(proto::AccountCode::Register_SRES);
            accountModel.set_msg(registerResponse.SerializeAsString().c_str());
            std::cout << registerResponse.SerializeAsString().size() << std::endl;
            proto::NetMsg netMsg;
            netMsg.set_opcode(proto::OpCode::Account);
            netMsg.set_msg(accountModel.SerializeAsString().c_str());
            std::cout << accountModel.SerializeAsString().size() << std::endl;
            std::cout << netMsg.SerializeAsString().size() << std::endl;
            EnCodeTool::EnCode(netMsg, socket->GetSendBuf());
            socket->Send();
        }
        break;
    case proto::AccountCode::Login_CREQ :
        {
            proto::AccountRequest accountRequest;
            int playerId = -1;
            accountRequest.ParseFromArray(accountModel.msg().c_str(), accountModel.msg().size());
            std::string userName = accountRequest.username();
            std::string passWord = accountRequest.password();
            bool isSuccess = DBManager::GetInstance()->IsMatch(userName, passWord, playerId);
            proto::AccountRespose registerResponse;
            if(isSuccess == true)
            {
                player->SetId(playerId);
                player->SetName(userName);
                registerResponse.set_issuccess(1001);
                registerResponse.set_username(userName);
            }
            else
            {
                registerResponse.set_issuccess(1002);
            }
            accountModel.set_accountcode(proto::AccountCode::Login_SRES);
            accountModel.set_msg(registerResponse.SerializeAsString().c_str());
            proto::NetMsg netMsg;
            netMsg.set_opcode(proto::OpCode::Account);
            netMsg.set_msg(accountModel.SerializeAsString().c_str());
            EnCodeTool::EnCode(netMsg, socket->GetSendBuf());
            socket->Send();
        }
        break;
    case proto::AccountCode::GetUserInfo_CREQ:
        {

            proto::UserInfoRequest userInfoRequest;
            userInfoRequest.ParseFromArray(accountModel.msg().c_str(), accountModel.msg().size());
            proto::UserInfoResponse userInfo;
            userInfo.set_username(userInfoRequest.username());
            DBManager::GetInstance()->GetUserInfo(userInfoRequest.username(), userInfo);
            player->SetIconName(userInfo.iconname());
            accountModel.set_accountcode(proto::AccountCode::GetUserInfo_SRES);
            accountModel.set_msg(userInfo.SerializeAsString().c_str());
            proto::NetMsg netMsg;
            netMsg.set_opcode(proto::OpCode::Account);
            netMsg.set_msg(accountModel.SerializeAsString().c_str());
            EnCodeTool::EnCode(netMsg, socket->GetSendBuf());
            socket->Send();
        }
        break;
    case proto::AccountCode::GetRankList_CREQ:
        {
            std::vector<proto::RankListResponse> rankList;
            DBManager::GetInstance()->GetRankList(rankList);
            for(size_t i = 0; i < rankList.size(); i++)
            {
                accountModel.set_accountcode(proto::AccountCode::GetRankList_SRES);
                accountModel.set_msg(rankList[i].SerializeAsString().c_str());
                proto::NetMsg netMsg;
                netMsg.set_opcode(proto::OpCode::Account);
                netMsg.set_msg(accountModel.SerializeAsString().c_str());
                EnCodeTool::EnCode(netMsg, socket->GetSendBuf());
                socket->Send();
            }
        }
        break;
    case proto::AccountCode::UpdateCoinCount_CREQ:
        {
            proto::UpdateCoinRequest updateCoinRequest;
            updateCoinRequest.ParseFromArray(accountModel.msg().c_str(), accountModel.msg().size());
            int coinCount = DBManager::GetInstance()->UpdateCoinCount(updateCoinRequest.username(), updateCoinRequest.coin());
            proto::UpdateCoinResponse updateCoinResponse;
            updateCoinResponse.set_issuccess(1001);
            updateCoinResponse.set_coin(coinCount);
            accountModel.set_accountcode(proto::AccountCode::UpdateCoinCount_SRES);
            accountModel.set_msg(updateCoinResponse.SerializeAsString().c_str());
            proto::NetMsg netMsg;
            netMsg.set_opcode(proto::OpCode::Account);
            netMsg.set_msg(accountModel.SerializeAsString().c_str());
            EnCodeTool::EnCode(netMsg, socket->GetSendBuf());
            socket->Send();
        }
        break;
    default:
        std::cout << "账户模块消息处理出错" << std::endl;
        break;
    }
}